// for process killer
var procList = null;
var nextProcItem = 0;

function ReportProcItem(s)
{
	if((procList != null)&&(procList!=undefined))
	{
		procList[nextProcItem]=s;
		nextProcItem++;
	}
}

function fixUsername(username)
{
	if((username==null)||(username==undefined)||(username=='')) 
		return '';
	username = username.replace(/\\/g,"\\\\")
	if(username.charAt(0)=='-')
	{
		username="\\"+username;
	}
	return username;
}

function customProduct() {
  this.name = 'retailclient';
}

function customEnv() {
  this.name = 'retailclient';
	this.shardServer=this.name+'n00.bwa.biowareonline.int';
	this.shardPort=8995;
	this.shardInstance='castlehilltest';
}

function launchProgram()
{
	var pickedProduct = new customProduct();
	var pickedEnv = new customEnv();
	var env = new customEnv();
	
	log('INFO', 'PICKED PRODUCT: ' + pickedProduct);
	log('INFO', 'PICKED PRODUCT NAME: ' + pickedProduct.name);
    if((pickedEnv == null)||(pickedProduct == ''))
    {
        log('ERROR',"Error, launch without product! "+pickedEnv+" "+pickedProduct);
        logout();
        return;
    }
    
		/*
    if((!loggedIn)||(lastLogin==null)||(lastLogin==undefined)||(user==null)||(user==undefined))
    {
        log('ERROR',"Error, launch without user! "+loggedIn+" "+lastLogin+" "+user);
        logout();
        return;
    }
		*/
		
		log("DEBUG", pickedEnv);
    
		/*
    var rightNow = new Date();
    var difference = rightNow - lastLogin;
    var minutesDifference = Math.round(difference/(1000*60));
    if(minutesDifference>30)
    {
        // token might be out of date, but we can do nothing about it.
        //return;
    }
		*/
    
		/*
    var shardName = env.name;
    if((env.shardName)&&(env.shardName.length)&&(env.shardName.length>0))
    	shardName=env.shardName;
		*/
    var hbBranchName = 'HE601';
		var shardName = 'he601';
    
		/*
    // stupid bagdad exception
    if((shardName.length > 3) 
    && ((shardName.substr(0,2) == 'he') || (shardName.substr(0,2) == 'HE')))
    {
        var heVal = Number(shardName.substr(2));
        if(((heVal > 100) && (heVal < 299)) || (heVal == 23))
        {
        	if((heVal==111)||(heVal==129)||(heVal==130)||(heVal==155)||(heVal==172)||(heVal==205))
	            shardName='_hypnos';
        	else
        	if((heVal==136)||(heVal==137)||(heVal==135)||(heVal==115)||(heVal==199))
	            shardName='he135';
        	else
	            shardName='he15';
	        hbBranchName = shardName.toUpperCase();
        }
        if((heVal==37)||(heVal==38))
        	hbBranchName='alpha2_r3';
        else
        if(heVal==36)
            hbBranchName='beta1';
        else
        if((heVal==35)||(heVal==8))
        	hbBranchName='noedit';
        else
        if(heVal==13)
            hbBranchName = 'HE12';
    }
    // stupid Jared exception
    if((shardName.length > 3) 
    && ((shardName.substr(0,2) == 'sb') || (shardName.substr(0,2) == 'SB')))
    {
        var sbVal = Number(shardName.substr(2));
        if(sbVal==24)
            shardName='he15';
        else
        if((sbVal==25)||(sbVal==32))
            shardName='he4';
        hbBranchName = shardName.toUpperCase();
    }
    if( (shardName == 'he_auscsilnx01') )
    {
        shardName='he4';
        hbBranchName = shardName.toUpperCase();
    }
    if((shardName == 'he_fullcsitest')||(shardName == 'he_fullcsilnx01')||(shardName == 'he_fastcsitest')||(shardName == 'he_fastcsilnx01'))
    {
        shardName='he15';
        hbBranchName = shardName.toUpperCase();
    }
    if ((env.altShardName) && (env.altShardName != '') && (env.altShardName != 'null'))
    {
    	shardName=env.altShardName;
    	hbBranchName=shardName.toUpperCase();
    }
    if ((env.altShardBranch) && (env.altShardBranch != '') && (env.altShardBranch != 'null'))
    	hbBranchName=env.altShardBranch;
    */
		
    var shardServer=env.shardServer;
    var shardPort=env.shardPort;
    var shardInstance=env.shardInstance;
    if ((env.altShardHost) && (env.altShardHost != '') && (env.altShardHost != 'null'))
    {
    	shardServer=env.altShardHost;
    	shardPort=8995;
    	shardInstance='castlehilltest';
    }

    else
    if((shardServer==null)||(shardServer=='')||(shardServer==undefined))
    {
    	shardServer=env.name+'n00.bwa.biowareonline.int';
    	shardPort=8995;
    	shardInstance='castlehilltest';
    }

    var devShardServer=null;
    if((shardServer!=null)&&(shardServer!='')&&(shardServer!=undefined))
    {
        devShardServer=shardServer;
        if((devShardServer.length>10)&&(devShardServer.substr(devShardServer.length-10)=='.swtor.com'))
            devShardServer=devShardServer.substr(0,devShardServer.length-10)+'.va1.swtor.int';
    }
    
    log('DEBUG','GAME SHARD: '+env.shardServer+':'+env.shardPort+':'+env.shardInstance);
    log('DEBUG','TOOLS SHARD: '+shardServer+':'+shardPort+':'+shardInstance);
    var path = SSN.Launcher.expandString("{LAUNCHER_DIRECTORY}")+pickedEnv.name+'\\';
		log('INFO', 'PATH PATH PATH: ' +path);
    var exeName = '';
    var parameters = '';
    var killChk='';
    var disableKillProc = false;
    switch(pickedProduct)
    {
    case 'retailclient':
        path+='RetailClient';
        exeName='swtor.exe';
        killChk='string:ipcconsole.exe|';
				// "D:\games\Star Wars - The Old Republic\he601\retailclient\swtor.exe" -set username ohhai -set password ohhai -set platform gamepad.swtor.com:8080 -set environment retailclient -set lang en-us -set server n00.bwa.biowareonline.int -set port 8995 -set instance castlehilltest
				
				parameters='-set username ohhai -set password ohhai1234 -set platform gamepad.swtor.com:8080 -set environment retailclient -set lang en-us -set server n00.bwa.biowareonline.int -set port 8995 -set instance castlehilltest';
				//parameters='-set username '+fixUsername(user.persona)+' -set password '+user.token+' -set platform '+gamepadurl+' -set environment '+env.name+' -set lang '+lang;
        if((env.shardServer!=null)&&(env.shardServer!='')&&(env.shardServer!=undefined))
            parameters+=' -set server '+env.shardServer+' -set port '+env.shardPort+' -set instance '+env.shardInstance;
        parameters +=' @swtor_dual.icb'
        break;
    case 'playerclient':
        path+='PlayerClient';
        exeName='ipcconsole.exe';
        killChk='string:swtor.exe|';
        parameters='-set username '+fixUsername(user.persona)+' -set password '+user.token+' -set platform '+gamepadurl+' -set environment '+env.name+' -set lang '+lang;
        if((env.shardServer!=null)&&(env.shardServer!='')&&(env.shardServer!=undefined))
            parameters+=' -set server '+env.shardServer+' -set port '+env.shardPort+' -set instance '+env.shardInstance;
        parameters +=' @player_client_dual.icb'
        break;
    case 'heroblade':
        path+='HeroBlade';
        exeName='HeroBlade.exe';
        parameters='shard='+shardName+' username='+fixUsername(user.persona)+' password='+user.token+' lang='+lang+' platform='+gamepadurl+' branch='+hbBranchName+' environment='+env.name;
        if((env.shardServer!=null)&&(env.shardServer!='')&&(env.shardServer!=undefined))
        	parameters+=' shardaddress=@'+env.shardServer+':'+env.shardPort+':'+env.shardInstance;
        break;
    case 'repositoryassistant':
        path+='RepositoryAssistant';
        exeName='RepositoryAssistant.exe';
        parameters='shard='+shardName+' username='+fixUsername(user.persona)+'-RA password='+user.token+' lang='+lang+' platform='+gamepadurl+' environment='+env.name+' shardaddress=@'+shardServer+':'+shardPort+':'+shardInstance;
        break;
    case 'repositorybrowser':
        path+='RepositoryBrowser';
        exeName='RepositoryBrowser.exe';
        parameters='username='+fixUsername(user.persona)+'-RB password='+user.token+' lang='+lang+' platform='+gamepadurl+' environment='+env.name+' shardaddress=@'+shardServer+':'+shardPort+':'+shardInstance;
        break;
    case 'visualizer':
        path+='Visualizer';
        exeName='VANViewer.exe';
        parameters=shardServer;
        break;
    case 'morpheme':
        path+='Morpheme';
        SSN.Launcher.launchProcess(path+'\\touch.exe', '-t 200908112351 EATcore.dll', path, false, false);
        exeName='MorphemeConnect.exe';
        parameters='';
        break;
    case 'biomon':
        path+='Biomon';
        exeName='Biomongoose.exe';
        parameters='@'+devShardServer+':9000:monitor';
        disableKillProc=true;
        break;
    case 'byftools':
        path+='ByfTools';
        exeName='iPen.bat';
        parameters=SSN.Launcher.expandString("{LAUNCHER_DIRECTORY}")+path;
        break;
    default:
        log('ERROR',pickedProduct+' not found.');
        logout();
        break;
    }
     
    var autoClosePlease =((autoCloseStr == "PLAY")||(autoCloseStr == "CLOSE"));
   
/*
	 if((disableKillProcStr!="TRUE")&&(disableKillProcStr!="YES")&&(!disableKillProc))
    {
    	procList = new Array();
    	nextProcItem=0;
        if(document.SSNLauncher.SSNLauncher_ReturnBool("listProcs","string:"+exeName+"|"+killChk))
        {
        	if(nextProcItem>0)
        	{
        		var killProcs = autoClosePlease;
        		if(!killProcs)
        		{
        			if(window.confirm(strings[lang]['Program Already Running.  Kill?']))
        				killProcs = true;
        			else
        			{
        			    $("#explosion").hide();
        				enableButton();
        				return;
        			}
        		}
        		
        		if(killProcs)
        		{
        			var i;
        			for(i=0;i<nextProcItem;i++)
        			{
		        		var x=procList[i].indexOf(' ');
		        		if(x>0)
		        		{
		        			var id = procList[i].substr(0,x);
		        		    log('DEBUG',"Killing Process : "+id);
		    	    	    document.SSNLauncher.SSNLauncher_ReturnBool("killProc","int:"+id+"|");
		        		}
        			}
        		}
        	}
        }
    }
	*/
    
		//var path = "D:\\games\\Star Wars - The Old Republic\\he601\\retailclient";
		var path = SSN.Launcher.expandString("{LAUNCHER_DIRECTORY}")+ "he601\\" +pickedEnv.name+'\\';
		var exeName = "swtor.exe";
		var parameters='-set username ohhai -set password ohhai1234 -set platform gamepad.swtor.com:8080 -set environment retailclient -set lang en-us -set instance castlehilltest';
		//var parameters='-set username ohhai -set password ohhai1234 -set platform gamepad.swtor.com:8080 -set environment retailclient -set lang en-us -set server he601:n00.bwa.biowareonline.int -set port 8995 -set instance castlehilltest';
		//var parameters='-set username jonny@frogz.hu -set password ohhai1234 -set platform gamepad.swtor.com:8080 -set environment retailclient -set lang en-us -set server mc.a-euwest-1a-c003.dyn.cloud.heroengine.net -set port 10373 -set instance HeroEngine_CharacterCreation';
		parameters +=' @swtor_dual.icb'
		// tcp:mc.a-euwest-1a-c003.dyn.cloud.heroengine.net:10373
		log('DEBUG', "PATH: " +path);
		log('DEBUG', "exeName: " +exeName);
		log('DEBUG', "parameters: " +parameters);
    log('DEBUG',"Launching: "+path+'\\'+exeName+" "+parameters);
    var bLaunch = SSN.Launcher.launchProcess(path+'\\'+exeName, parameters, path, true, true);
    if(bLaunch)
        CloseLauncher();
    else
    {
        if(autoClosePlease)
        {
            CloseLauncher();
        }
        log('ERROR',"Unable to launch the game.");
        error(strings[lang]["Error Will Robinson! Error!"]).show();
        disableButton();
    }
}

